//
//  MZFineViewController.m
//  MZ_CT
//
//  Created by lanou3g on 15/12/15.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "MZFineViewController.h"
#import "FineCell.h"
#import "YPMakeURL.h"
#import "YPNetWorking.h"
#import "FineHelper.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "YPWebViewVC.h"
#import "YPJudgeNet.h"
@interface MZFineViewController ()

@property(nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,assign) NSInteger number;

@end

@implementation MZFineViewController
static NSInteger flag = 0;
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([[YPJudgeNet shareJudgeNet]isConnection] == NO) {
        UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络异常，请检查网络" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [controller addAction:action];
        [self presentViewController:controller animated:YES completion:nil];
    }else{
        self.number = 0;
        [self.tableView registerClass:[FineCell class] forCellReuseIdentifier:@"cell"];
        [self setupRefresh];
        [self addData];
        self.dataArr = [NSMutableArray array];
    }
}

-(void)setupRefresh{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
}


-(void)footAction{
    flag += 10;
    [[YPNetWorking shareNetWorking] getDictWithURL:[YPMakeURL shareMakeURL].shopURL dict:[[YPMakeURL shareMakeURL] makeShopDetailDicWithNum:flag] success:^(NSDictionary *dict) {
        for (NSDictionary *dic in dict[@"data"][@"list"]) {
            FineHelper *model = [[FineHelper alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArr addObject:model];
            //NSLog(@"%ld",self.dataArr.count);
            [self.tableView reloadData];
        }
        
    } fail:^(NSError *err) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }];
}

-(void)headerAction{
    [self.tableView.mj_header endRefreshing];
}

-(void)addData{
   
    [[YPNetWorking shareNetWorking] getDictWithURL:[YPMakeURL shareMakeURL].shopURL dict:[YPMakeURL shareMakeURL].shopDic success:^(NSDictionary *dic) {
        for (NSDictionary *dict in dic[@"data"][@"list"]) {
            FineHelper *model = [[FineHelper alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataArr addObject:model];
            [self.tableView reloadData];
        }
    } fail:^(NSError *err) {
        NSLog(@"%@",err);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    FineHelper *model = self.dataArr[indexPath.row];
    self.number++;
    if (self.number >= flag + 9) {
      self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footAction)];
    }
    cell.FineLable.text = model.title;
    [cell.FineImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPat{
    YPWebViewVC *webVc = [[YPWebViewVC alloc]init];
    FineHelper *model = self.dataArr[indexPat.row];
    NSString *url = [NSString stringWithFormat:@"http://pianke.me/webview/%@",model.contentid];
    webVc.tingID = url;
    webVc.contendId = model.contentid;
    webVc.type = @"良品";
    webVc.shareImage = model.coverimg;
    webVc.shareTitle = model.title;
    [self.navigationController presentViewController:[[UINavigationController alloc]initWithRootViewController:webVc] animated:YES completion:nil];
}
@end
